<?php
namespace Home\Model;
use Think\Model;
use Common\Common\Classes\Message;

class CourseService extends Model{
    private $cou_sql;
    public function __construct(){
        $this->cou_sql = D("Course");
    }
    //---------------------------------增加一个课程---------------------------------
    public function addCourse(){                    //增加一个课程
        $_POST["teacher_tea_id"] = $_SESSION["id"];
        if($this->checkName()) {
            if ($this->cou_sql->create($_POST, 1)) {
                $this->cou_sql->add($_POST);
                $data = new Message(1,"新增课程成功!","");
            } else {
                $data = new Message(0,$this->cou_sql->getError(),"");
            }
        }
        else{
            $data = new Message(0,"该课程已经存在!","");
        }
        return $data;
    }
    //---------------------------------检测是否存在同名课程课程(同个老师的)---------------------------------
    public function checkName(){                 //检测是否存在同名课程(同个老师的)
        $where["teacher_tea_id"] = $_SESSION["id"];
        $where["cou_name"] = $_POST["cou_name"];
        if($this->cou_sql->where($where)->find()){
            return false;
        }
        return true;
    }
    //---------------------------------检测课程能否直接删除---------------------------------------------
    public function checkDelete(){
        $where["cou_id"] = $_POST["cou_id"];
        $class = $this->cou_sql->relation("class")->where($where)->find();
        if(count($class["class"])==0){
            $data = new Message(1,"课程可以直接删除!","");
        }
        else{
            $data = new Message(0,"课程不能直接删除!","");
        }
        return $data;
    }
    //---------------------------------删除某个课程---------------------------------------------
    public function deleteCourse(){
        if($this->cou_sql->delete($_POST["cou_id"])!=""){
            $data = new Message(1,"课程删除成功!","");
        }
        else{
            $data = new Message(0,"课程删除失败!","");
        }
        return $data;
    }
    //---------------------------------修改某个课程信息---------------------------------------------
    public function updateCourse(){
        if($this->cou_sql->create($_POST,3)){
            $where["cou_id"] = $_POST["cou_id"];
            $update["cou_code"] = $_POST["cou_code"];
            $update["is_available"] = $_POST["is_available"];
            $this->cou_sql->where($where)->save($update);
            $data = new Message(1,"课程信息修改成功!","");
        }
        else{
            $data = new Message(0,$this->cou_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------查看登陆者是否有权限查看某个课程的信息---------------------------------
    public function checkBelong(){
        $where["cou_id"] = $_GET["cd"];
        $where["teacher_tea_id"] = $_SESSION["id"];
        if($this->cou_sql->create($where,4)) {
            if ($this->cou_sql->where($where)->find()!="") {
                $data = new Message(1, "你有权限查看!", "");
            } else {
                $data = new Message(0, "你没有权限查看该课程下的信息!", "");
            }
        }
        else{
            $data = new Message(0,$this->cou_sql->getError(), "");
        }
        return $data;
    }
    //---------------------------------得到所有某个老师的所有课程---------------------------------
    public function getAllCourse($field) {
        $where["teacher_tea_id"] = $_SESSION["id"];
        return $this->cou_sql->where($where)->field($field)->order('cou_id desc')->select();
    }
    //---------------------------------获得某个老师的课程数量---------------------------------
    public function getPageInfo(){               //获得某个老师的课程数量
        $where["teacher_tea_id"] = $_SESSION["id"];
        $pageInfo["count"] = $this->cou_sql->where($where)->count();
        $pageInfo['page'] = ceil(intval($pageInfo["count"]) / 10);                          //获得总得页数
        $pageInfo['np'] = $_GET["np"];
        if($pageInfo['np'] <= 0 || $pageInfo['np'] > $pageInfo['page']){
            $pageInfo['np'] = 1;              //如果没有传当前页数过来就当第一页来处理
        }
        return $pageInfo;
    }
    //---------------------------------获得某个老师的10个课程的基本信息---------------------------------
    public function getTenCoursesInfo($now_page,$field){    //获得某个老师的10个课程的基本信息
        $where["teacher_tea_id"] = $_SESSION["id"];
        $course = $this->cou_sql->where($where)->field($field)->limit(($now_page-1)*10,10)->order('cou_id desc')->select();
        return $course;
    }
    //---------------------------------检测是否有权限对课程进行操作-----------------------------------
    public function  checkPower(){
        $where["teacher_tea_id"] = $_SESSION["id"];
        if($_GET["cd"]!=""){
            $where["cou_id"] = $_GET["cd"];
        }
        else if($_POST["cou_id"] != "") {
            $where["cou_id"] = $_POST["cou_id"];
        }
        $result = $this->cou_sql->where($where)->find();
        if($result != ""){
            $data = new Message(1,"你拥有权限对该课程进行操作!","");
        }
        else{
            $data = new Message(0,"你没有权限对该课程进行操作!","");
        }
        return $data;
    }
    public function __destruct(){}
}